package com.ts.api.module.common.controller;

import com.gitee.apanlh.web.model.vo.ResultVO;
import com.ts.api.common.context.ChannelRequestContext;
import com.ts.api.common.localcache.DistrictCache;
import com.ts.api.module.common.entity.DistrictPO;
import com.ts.api.module.common.entity.bo.IpInfoBO;
import com.ts.api.module.common.entity.vo.RespIpInfoVO;
import com.ts.api.module.common.service.IpInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *  获取信息相关控制层
 *
 *  @author Pan
 */
@RestController
@RequestMapping("/common/get/info")
public class GetInfoController {

    @Autowired
    private IpInfoService ipInfoService;

    /**
     * 根据ip置换用户信息
     *
     * @return AjaxResult
     */
    @GetMapping("/ip")
    public ResultVO<Object> getIpInfo() {
        String ip = ChannelRequestContext.get().getIp();
        //  本机地址返回
        if (ip.startsWith("127.") || ip.startsWith("192.") || ip.startsWith("172.")) {
            RespIpInfoVO respIpInfoVO = new RespIpInfoVO();
            respIpInfoVO.setProvince("福建省");
            respIpInfoVO.setCity("福州市");
            respIpInfoVO.setAdcode("350100");
            return ResultVO.suc(respIpInfoVO);
        }

        //  置换ip信息
        IpInfoBO ipInfoBO = ipInfoService.getByIp(ip);
        //  获取地区缓存
        DistrictPO districtPO = DistrictCache.getByName(ipInfoBO.getCity());
        //  返回信息
        RespIpInfoVO respIpInfoVO = new RespIpInfoVO();
        respIpInfoVO.setProvince(ipInfoBO.getProvince());
        respIpInfoVO.setCity(ipInfoBO.getCity());
        respIpInfoVO.setAdcode(districtPO.getCode());
        return ResultVO.suc(respIpInfoVO);
    }
}
